<?
include_once("adminBase.php");

class Suggest extends AdminBase{
	function index()
	{
		$sql = "select count(*)as total from tbl_suggest where del_flg='0'";
		$total = $this->db->loadResult($sql);
		$curPage = @intval($_REQUEST['p']);
		if($curPage < 1) $curPage = 1;
		$showNum = _BACKVIEWNUM;
		$pageLink = "/management/admin_suggest.php";
		$page = new PageClass($total,$curPage,$showNum,$pageLink);
		$limitStart = $page->limitStart;
		$this->smarty->assign("pageList",$page->pageList);
		
		$sql = "SELECT ts.*,tsa.content as answer FROM `tbl_suggest` ts left join `tbl_suggest_answer` tsa on tsa.sug_id=ts.id where ts.del_flg='0' order by ts.create_time desc limit $limitStart,$showNum";
		$this->db->setQuery($sql);
		$data = $this->db->loadList();
		$this->smarty->assign("row",$data);
		adminDisplay("admin_suggest.tpl");
	}
	function del()
	{
		$id = intval($_REQUEST['id']);
		$sql = "update tbl_suggest set del_flg=1 where id='$id'";
		if($this->db->query($sql)){
			echo "ok";
		}
		die;
	}
	function delAll()
	{
		$ids = $_REQUEST['selectId'];
		$sql = "update tbl_suggest set del_flg=1 where id in ('".implode("','",$ids)."')";
		$this->db->query($sql);
		$this->index();
	}
	function delCompletely()
	{
		$ids = $_REQUEST['selectId'];
		$sql = "delete from tbl_suggest where id in ('".implode("','",$ids)."')";
		$this->db->query($sql);
		$this->index();
	}
	function answer()
	{
		$sug_id = intval($_REQUEST['id']);
		$content = urldecode($_REQUEST['content']);
		if($sug_id > 0 && $content != "")
		{
			$sql = "update tbl_suggest set is_ans='1' where id='$sug_id'";
			$this->db->query($sql);
			$sql = "select id from tbl_suggest_answer where sug_id='$sug_id'";
			$id = $this->db->loadResult($sql);
			if($id > 0){
				$temprow['content'] = $content;
				$where = " where sug_id='$sug_id'";
				$this->db->update($temprow,"tbl_suggest_answer",$where);
				echo "ok";
			}else{
				$temprow['sug_id'] = $sug_id;
				$temprow['content'] = $content;
				$this->db->insert($temprow,"tbl_suggest_answer");
				echo "ok";
			}
		}
		die;
	}
}

$suggest = new Suggest();